<template>
  <div>
    <el-upload
      v-loading="isLoading"
      element-loading-text="文件上传中..."
      drag
      :http-request="checkedFile"
      action="/"
      :multiple="true"
      :before-upload="beforeAvatarUpload"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">
        将文件拖到此处，或
        <em>点击上传</em>
      </div>
    </el-upload>
    <el-progress v-if="isView" style="max-width: 360px" :text-inside="true" :stroke-width="20" :percentage="percentage" />
  </div>
</template>
<script>
// var tus = require('tus-js-client')
import axios from 'axios'
export default {
  props: {
    name: {
      type: String,
      default: () => ''
    },
    type: {
      type: String,
      default: () => ''
    }
  },
  // props: ['name', 'type'],
  data() {
    return {
      upload: null,
      percentage: 0,
      isView: false,
      fileId: null,
      flieType: {
        apk: { message: '只能上传APK安装包文件', type: ['application/vnd.android.package-archive'] },
        img: { message: '只能上传PNG,JPEG,JPG图片文件', type: ['image/png', 'image/jpeg'] }
      },
      isLoading: false
    }
  },
  created() {
    console.log(this.name)
  },
  methods: {
    checkedFile(file) {
      const that = this
      this.isLoading = true
      var formData = new FormData()
      formData.append('files', file.file)

      const token = this.$store.state.login.userInfo ? this.$store.state.login.userInfo.token : null
      axios({
        method: 'post',
        data: formData,
        timeout: 500000,
        url: process.env.VUE_APP_BASE_API + '/api/System/FTPAPKUpload',
        headers: {
          'Content-type': 'application/json;charset=UTF-8',
          version: '1.0.0',
          Authorization: `Bearer ${token} `
        }
      })
        .then(function(res) {
          that.$emit('input', that.name, res.data.content)
          that.$message({
            message: '文件上传成功！',
            type: 'success'
          })
        })
        .finally(() => {
          that.isLoading = false
        })
      // this.api
      //   .FTPAPKUpload(formData)
      //   .then(res => {
      //     this.$emit('input', this.name, res)
      //     this.$message({
      //       message: '文件上传成功！',
      //       type: 'success'
      //     })
      //   })
      //   .finally(() => {
      //     this.isLoading = false
      //   })
    },
    beforeAvatarUpload(file) {
      const isCorrectType = this.flieType[this.type].type.includes(file.type)
      if (!isCorrectType) {
        this.$message.error(this.flieType[this.type].message)
      }
      return isCorrectType
    }
  }
}
</script>
